package saleweixin.invocation;

import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import play.Logger;
import saleweixin.WxMpContext;
import saleweixin.WxMpInvocation;

/**
 * 取消关注公众号.
 */
public class UnsubscribeInvocation extends WxMpInvocation {

    @Override
    protected WxMpXmlOutMessage doExecute(WxMpContext context) {
//        WebUser wxUser = WebUser.findByOpenId(context.inMessage.getFromUserName());
//        if(wxUser != null) {
//            wxUser.subcribed = Boolean.FALSE;
////            wxUser.subscribedAt = new Date();
//            wxUser.save();
////            new UserHistory(wxUser , UserHistoryType.UN_SUBSCRIBE , "用户取消关注");
//        } else {
//            Logger.warn("LOG00710: 没有找到对应的用户(openId:%s)，可能数据问题", context.inMessage.getFromUserName());
//        }
        Logger.info("取消用户关注 :%s" , context.inMessage.getFromUserName());
        return WxMpXmlOutMessage.TEXT()
                .fromUser(context.inMessage.getToUserName())
                .toUser(context.inMessage.getFromUserName())
                .content("取消关注成功 取消关注将不能再领红包")
                .build();
    }

    @Override
    protected boolean matchEvent(String event, String eventKey) {
        return "unsubscribe".equals(event);
    }

}
